﻿using Data;
using GlobalCode;
using Noesis.Javascript;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace JavaScript
{
    static class BuildAllGlobalCode
    {
        public static void BuildGlobalCode(JavascriptContext context)
        {
            string thisExeDirectory = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string thisDirectory = thisExeDirectory + GlobalDirectory.SaveDirectory + "\\JSONCodeDataList";
            DirectoryInfo TheFolder = new DirectoryInfo(thisDirectory);
            foreach (FileInfo NextFile in TheFolder.GetFiles())
            {
                string thisFile = thisDirectory + "\\" + NextFile.Name.TrimEnd(NextFile.Extension.ToCharArray()) + ".json";
                codelist NodeDatalist = null;
                if (File.Exists(thisFile))
                {
                    using (StreamReader file = File.OpenText(thisFile))
                    {
                        Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
                        NodeDatalist = (codelist)serializer.Deserialize(file, typeof(codelist));
                        NodeDatalist.opendnumber = 1;
                        context.Run(NodeDatalist.Data);
                    }
                }
            }
        }
    }
}
